package com.zhouwen.ssm.resolver;

import com.zhouwen.ssm.exception.MyException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义异常处理步骤
 * ①创建异常处理器类实现HandlerExceptionResolver
 * ②配置异常处理器
 * ③编写异常页面
 * ④测试异常跳转
 */
public class MyExceptionResovler implements HandlerExceptionResolver {

    /**
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e   异常对象
     * @return mv 跳转到错误视图信息
     */
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView mv = new ModelAndView();

        if (e instanceof MyException) {
            mv.addObject("info","自定义异常");
        } else if (e instanceof ClassCastException) {
            mv.addObject("info","类型转换异常");
        }

        mv.setViewName("error");
        return mv;
    }
}
